<!DOCTYPE HTML>
<html>
<HEAD>
<meta charset="utf-8">
<TITLE>员工添加/修改</TITLE>
</HEAD>
<body>
    
<?php
include_once("base-class.php");

//新建sae数据库类
$mysql = new SaeMysql();

//获取需要修改的员工ID号传入
$roster_id=intval($_GET["roster_id"]);

//获取操作标识传入
$action=$_POST["action"];
$action= string::un_script_code($action);
$action= string::un_html($action);

//判断是否修改，如果传入了员工ID，进行数据库查询获取全部内容
if($roster_id)
{
	$roster_value=$mysql->getLine("select * from roster where roster_id=$roster_id");
    if(!$roster_value)
	{
		echo "<script>alert('无此员工');history.back();</Script>";
		exit;
	}
}

//如果获取到操作标识，进行录入或者修改操作
if($action=="update")
{
    //获取表单传入数据
	$old_roster_id=$_POST["roster_id"];
	$roster_name=$_POST["roster_name"];
    $roster_py=$_POST["roster_py"];
	$roster_number=$_POST["roster_number"];
	$roster_sex=$_POST["roster_sex"];
	$roster_mp=$_POST["roster_mp"];
	$roster_phone=$_POST["roster_phone"];
	$roster_class=$_POST["roster_class"];
	$roster_mail=$_POST["roster_mail"];
    $roster_status=$_POST["roster_status"];

    //传入数据过滤
    $old_roster_id=intval($old_roster_id);
    $roster_name= string::un_script_code($roster_name);
    $roster_py= string::un_script_code($roster_py);
    $roster_number= string::un_script_code($roster_number);
    $roster_sex= intval($roster_sex);
    $roster_mp= string::un_script_code($roster_mp);
    $roster_phone= string::un_script_code($roster_phone);
    $roster_class= intval($roster_class);
    $roster_mail= string::un_script_code($roster_mail);
    $roster_status= intval($roster_status);
    
    //检测必填项目
    if(!$roster_name || !$roster_py || !$roster_number || !$roster_sex || !$roster_mp 
       || !$roster_class || !$roster_status)
    {
		echo "<script>alert('表单未填完整，带星号为必填项！');history.back();</Script>";
		exit;    
    }
    
    //监测手机号
    
    if(!string::is_mobile($roster_mp))
    {
		echo "<script>alert('手机号码不正确，请重新输入！');history.back();</Script>";
		exit;    
    }
    //监测邮箱
    
    if(0)//(!string::is_mail($roster_mail))//改成非必填
    {
		echo "<script>alert('邮箱格式不正确，请重新输入！');history.back();</Script>";
		exit;    
    }
    
    
    //默认参数
    $nowtime=date("Y/m/d H:i:s",time());
    //如果是修改
    if($old_roster_id)
    {
 
  		$sql = "update roster set 
        roster_name='$roster_name',
        roster_py'$roster_py',
        roster_number='$roster_number',
        roster_sex='$roster_sex',
        roster_mp='$roster_mp',
        roster_phone='$roster_phone',
        roster_class='$roster_class',
        roster_mail='$roster_mail',
        roster_status='$roster_status',
        edittime='$nowtime'
        where roster_id=$old_roster_id";
 		$mysql->runSql( $sql );
    }
    else
    {
        //新增
   		$sql = "insert into roster 
        (roster_name,roster_py,roster_number,roster_sex,
        roster_mp,roster_phone,roster_class,
        roster_mail,roster_status,addtime,edittime,status) 
        values 
        ('$roster_name','$roster_py','$roster_number','$roster_sex',
        '$roster_mp','$roster_phone','$roster_class',
        '$roster_mail','$roster_status','$nowtime','$nowtime',1)";
 		$mysql->runSql( $sql );
   	
    }
    if( $mysql->errno() != 0 )
    {
        echo "<script>alert('".$mysql->errmsg() ."');history.back();</Script>";
        exit;
    }
    else
    {
        echo "<script>alert('操作成功！');location='roster_add.php?roster_id=$old_roster_id';</Script>";
        exit;    
    }
    
}    

//获取所有部门
$class_list=$mysql->getData("select class_name,class_id from class where status=1 order by class_fid asc");

?>
    <!--页面名称-->
	<h3>员工添加/修改<a href="roster_manager.php">返回>></a></h3>
    <p>带星号为必填项</p>
    <!--表单开始-->
    <form action="?" method="post" name="roster_add" id="roster_add" enctype="multipart/form-data">
        <p>
            <b>*</b>姓名：<input type="text" value="<?php echo $roster_value["roster_name"];?>" name="roster_name">
        </p>
        <p>
            <b>*</b>拼音首字母：<input type="text" value="<?php echo $roster_value["roster_py"];?>" name="roster_py">
        </p>
        <p>
            <b>*</b>工号：<input type="text" value="<?php echo $roster_value["roster_number"];?>" name="roster_number">
        </p>
        <p>
            <b>*</b>性别：
            <select name="roster_sex">
                <option value="0">请选择性别</option>
                <option <?php if($roster_value["roster_sex"]==1) echo "selected"?> value="1">男</option>
                <option <?php if($roster_value["roster_sex"]==2) echo "selected"?>  value="2">女</option>
            </select>
        </p>
        <p>
            <b>*</b>手机：<input type="text" value="<?php echo $roster_value["roster_mp"];?>" name="roster_mp">
        </p>
        <p>
            电话：<input type="text" value="<?php echo $roster_value["roster_phone"];?>" name="roster_phone">
        </p>
        <p>
            <b>*</b>部门：
            <select name="roster_class">
                <option value="0">请选择部门</option>
                <?php
    				//把所有部门列表出来
                    foreach($class_list as $value)
                    {
                        $class_select=($roster_value["roster_class"]==$value["class_id"])?" selected":"";
                        echo "<option value=\"$value[class_id]\" $class_select>$value[class_name]</option>";
                    }
                ?>
            </select>
        </p>
        <p>
            邮箱：<input type="text" value="<?php echo $roster_value["roster_mail"];?>" name="roster_mail">
        </p>
        <p>
            <b>*</b>状态：
            <select name="roster_status">
                <option value="0">请选择员工状态</option>
                <option <?php if($roster_value["roster_status"]==1) echo "selected"?> value="1">在职</option>
                <option <?php if($roster_value["roster_status"]==2) echo "selected"?>  value="2">休假</option>
                <option <?php if($roster_value["roster_status"]==3) echo "selected"?>  value="3">病假</option>
                <option <?php if($roster_value["roster_status"]==4) echo "selected"?>  value="4">离职</option>
            </select>
        </p>
         <p>
             <!--隐藏参数，用来放置操作标示和修改的ID-->
            <input type="hidden" name="action"  value="update">
            <input type="hidden" name="roster_id" value="<?=$roster_value["roster_id"]?>">
             <!--表单提交-->
            <input type="submit" value="提交" />
        </p>
    </form>
</body>
</html>
